<?php
if($_FILES['pic']['error'] !=0){
    $a['error'] = 1;
    $a['errMsg'] = "上传文件出错";
    echo json_encode($a);
    exit;
}
$filName = $_FILES['pic']['name'];
$fileExt = explode(".",$filName);
$ext = strtolower(end($fileExt));
$allowExt = array("jpg","jpeg","png","gif");
if(in_array($ext,$allowExt)){ //扩展名正确
    $picName = time().rand(1000,9999).".".$ext;
    $lastFileName = "./tx/".$picName;
    if(move_uploaded_file($_FILES['pic']['tmp_name'],$lastFileName)){
        $a['error'] = 0;
        $a['fileName'] = $picName;
    }
    else{
        $a['error'] = 1;
        $a['errMsg'] = "上传文件出错";
    }
}
else{  //文件类型错误
    $a['error'] = 1;
    $a['errMsg'] = "上传文件格式不允许，只能上传JPG、JPEG、PNG和GIF文件。";
}
echo json_encode($a);